import os, os.path
from django.core.management.base import BaseCommand, CommandError
from market.tests.runTests import initDatabase, testURLS, initBetrooms, saveBetroomsAndCompare, marketAt100AndComeBackToInitialPoint, testContractMarket
from django.test.client import Client
import getpass
import sys


##################################################################################################
# this command take the object in base and create associated text file.
# the data saved are those which will allow to reconstruct the base in the same state (except histories and comments)
class Command(BaseCommand):
    help = 'save the base in the specified directory'

    def handle(self, *args, **options):
        print '///////////////////////////////////////////////////////////////////////'
        print '\n\nSTARTING TESTS\n\n'
        print '///////////////////////////////////////////////////////////////////////'

        # init a fresh data base
        initDatabase()

        # get the password. It seems complicated to input a password for the super user when creating the data base.
        # we ask for it now so we are able to pass it and ask just once for it.
        print 'Please enter the password to modify the data base.'
        continueAsking =  True
        while continueAsking:
            password = getpass.getpass(stream=sys.stderr)
            c = Client()
            response = c.post('/login/', {'username': 'Jean', 'password': password})
            if int(response.status_code)!= 302:
                print 'Failed to login with this password. Either the loggin feature is broken, either you entered a wrong password. Maybe try again.'
            else:
                continueAsking =  False
                

        # test that the urls are avaialble
        print '\n\n///////////////////////////////////////////////////////////////////////'
        print 'Testing urls...'
        testURLS(password)
            
        #init the test betroom
        print '\n\n'
        print '///////////////////////////////////////////////////////////////////////'
        print 'Loading test betrooms from the test files...'
        testDirsAndbetroomNames = initBetrooms(password)
        
        #check that saving the base now will give back the input files
        print '\n\n///////////////////////////////////////////////////////////////////////'
        print 'Saving the betrooms to files and comparing them to files with which the bet rooms have been build...'
        saveBetroomsAndCompare( password, testDirsAndbetroomNames, os.path.join( os.getcwd(), 'market', 'tests', 'savedRoomFiles' ) )

        ##############################################################################################################################
        # For all the betroom with type 0, this perform the following test:
        # - register every player view and mtm
        # - change the view of all pplayer to 100 percent on the first asset.
        # - check that the market is 100% too
        # - set bacj the player view in the reverse order
        # - check that the market view and the player MtMs are the initial ones
        print '\n\n///////////////////////////////////////////////////////////////////////'
        print 'tests on proba market...'
        marketAt100AndComeBackToInitialPoint()

        ##############################################################################################################################
        # For all the betroom with type 1, this test that the average mtm remain 10000
        print '\n\n///////////////////////////////////////////////////////////////////////'
        print 'tests on contract market...'
        testContractMarket()
    
        # successssss!!!!!!!!!!!!!!!
        print '\n\n///////////////////////////////////////////////////////////////////////'
        print 'TESTS SUCCESSFULLY RUN!'
        return
